Codice Performante in C++ per R
foo.cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
06 luglio 2015
Codice Performante in C++ per R
foo.cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
library(Rcpp)
sourceCpp("foo.cpp")
timesTwo(2)
## [1] 4
RcppArmadillo Uso di Rcpp con la libreria di linear algebra in C++ Armadillo
#include <RcppArmadillo.h>
#include <RcppGSL.h> #include <gsl/gsl_matrix.h> #include <gsl/gsl_blas.h>
RcppSMC implementa Rcpp per Sequential Monte Carlo and Particle Filters (SMC)
#include <RcppParallel.h>
#include <Rcpp.h>
using namespace Rcpp;
#include <algorithm>
// [[Rcpp::export]]
double vectorSum(NumericVector x) {
return std::accumulate(x.begin(), x.end(), 0.0);
}
// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
#include <Rcpp.h>
using namespace RcppParallel;
struct Sum : public Worker
{
// source vector
const RVector<double> input;
// accumulated value
double value;
// constructors
Sum(const Rcpp::NumericVector input) : input(input), value(0) {}
Sum(const Sum& sum, Split) : input(sum.input), value(0) {}
// accumulate just the element of the range I've been asked to
void operator()(std::size_t begin, std::size_t end) {
value += std::accumulate(input.begin() + begin, input.begin() + end, 0.0);
}
// join my value with that of another Sum
void join(const Sum& rhs) {
value += rhs.value;
}
};
using namespace RcppParallel;
// [[Rcpp::export]]
double parallelVectorSum(Rcpp::NumericVector x) {
// declare the SumBody instance
Sum sum(x);
// call parallel_reduce to start the work
parallelReduce(0, x.length(), sum);
// return the computed sum
return sum.value;
}
library(RcppParallel)
sourceCpp("foo_ser.cpp")
sourceCpp("foo_par.cpp")
library(rbenchmark)
v <- as.numeric(c(1:10000000))
pp <- benchmark(vectorSum(v),parallelVectorSum(v),order="relative")
pp
## test replications elapsed relative user.self sys.self ## 2 parallelVectorSum(v) 100 0.418 1.000 1.483 0.009 ## 1 vectorSum(v) 100 1.181 2.825 1.166 0.005 ## user.child sys.child ## 2 0 0 ## 1 0 0
R embedded in classi C++
#include <RInside.h>
Pacchetto giovane, alle versione 0.2, ma promettente …
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
R["txt"] = "Hello, world!\n"; // assign a char* (string) to 'txt'
R.parseEvalQ("cat(txt)"); // eval the init string, ignoring any returns
exit(0);
}
Widgets interattivi costruiti con R e javascript Già ci sono alcuni paccheti di R che implementano librerie js
esempi con questo dataset
Scaricamenti prari di pacchetti R per nazione
m_hours_countries[1:4,1:10]
## AR AT AU BE BR CA CH CL CN CO ## 00:00 24 2 191 3 115 136 3 9 3045 55 ## 01:00 21 1 188 1 84 118 5 25 3300 48 ## 02:00 18 0 143 1 65 135 2 13 3698 42 ## 03:00 13 2 152 0 41 102 4 12 3252 34
Libreria per serie temporali
Heatmap usando D3
library(d3heatmap) m_counts <- t(acast(counts, date ~ hour, value.var = 'n')) d3heatmap(scale(m_hours_countries), cellnote = m_hours_countries, colors = "Blues", dendrogram = "col")
Mappe interattive
library(leaflet)
leaflet(quakes) %>%
addTiles("https://api.tiles.mapbox.com/v4/mapbox.wheatpaste/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoic2FudG9yb21hIiwiYSI6ImUxN2ZkZWRkNTU2NjVlZjRiM2E2ZmZhNGQyODM0OGFhIn0.mM32XaR8Ro0dBQIlA6G2iA") %>%
addCircles(color = "#CC0000", weight = 2, radius = ~10^mag / 5,
popup = ~as.character(stations))
Mappe interattive
## Assuming 'long' and 'lat' are longitude and latitude, respectively
Libreria 3D in javascript
library(threejs)
globejs(lat = quakes$lat, long = quakes$long,
value = 10^quakes$mag / 10^4,atmosphere = TRUE)
Tree Viewer in js D3
# devtools::install_github("sdwfrost/epiwidgets")
library(epiwidgets)
nwk <- "(((EELA:0.150276,CONGERA:0.213019):0.230956,(EELB:0.263487,CONGERB:0.202633):0.246917):0.094785,((CAVEFISH:0.451027,(GOLDFISH:0.340495,ZEBRAFISH:0.390163):0.220565):0.067778,((((((NSAM:0.008113,NARG:0.014065):0.052991,SPUN:0.061003,(SMIC:0.027806,SDIA:0.015298,SXAN:0.046873):0.046977):0.009822,(NAUR:0.081298,(SSPI:0.023876,STIE:0.013652):0.058179):0.091775):0.073346,(MVIO:0.012271,MBER:0.039798):0.178835):0.147992,((BFNKILLIFISH:0.317455,(ONIL:0.029217,XCAU:0.084388):0.201166):0.055908,THORNYHEAD:0.252481):0.061905):0.157214,LAMPFISH:0.717196,((SCABBARDA:0.189684,SCABBARDB:0.362015):0.282263,((VIPERFISH:0.318217,BLACKDRAGON:0.109912):0.123642,LOOSEJAW:0.397100):0.287152):0.140663):0.206729):0.222485,(COELACANTH:0.558103,((CLAWEDFROG:0.441842,SALAMANDER:0.299607):0.135307,((CHAMELEON:0.771665,((PIGEON:0.150909,CHICKEN:0.172733):0.082163,ZEBRAFINCH:0.099172):0.272338):0.014055,((BOVINE:0.167569,DOLPHIN:0.157450):0.104783,ELEPHANT:0.166557):0.367205):0.050892):0.114731):0.295021)"
treewidget(nwk)
Tree Viewer in js D3
library(tmap,quietly = T,warn.conflicts = F)
data(World)
data(metro)
qtm(World, fill="pop_est_dens", theme="World", fill.style="kmeans",
fill.title="Population per km^2") +
qtm(metro, bubble.size = "pop2010", bubble.col ="purple",
bubble.title.size="Metropolitan Areas", theme = "World", bubble.scale=.5)